package com.fangshi.api

import java.security.SecureRandom
import java.security.cert.X509Certificate
import javax.net.ssl.*

class SSLFactory {
    companion object {
        fun getHostnameVerifier(): HostnameVerifier = HostnameVerifier { _, _ ->
            true
        }

        @Throws(RuntimeException::class)
        fun getSSLSocketFactory(): SSLSocketFactory{
            try {
                val sslContext = SSLContext.getInstance("SSL")
                sslContext.init(null, getTrustManager(), SecureRandom())
                return sslContext.socketFactory;
            } catch (e: Exception) {
                throw RuntimeException(e)
            }

        }

        fun getTrustManager(): Array<X509TrustManager> {
            return arrayOf(object : X509TrustManager {
                override fun checkClientTrusted(
                    chain: Array<out X509Certificate>?,
                    authType: String?
                ) {
                }

                override fun checkServerTrusted(
                    chain: Array<out X509Certificate>?,
                    authType: String?
                ) {
                }

                override fun getAcceptedIssuers(): Array<X509Certificate> {
                    return emptyArray()
                }

            })

        }


    }
}